﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.Orders;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.Order;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.Order
{
    /// <summary>
    ///     订单主表仓库
    /// </summary>
    public class OrderMainRepository : SqlServerRespository<OrderMainInfoEntity>,
        IOrderMainRepository
    {
        private const string querySql = @"  ddlsh AS Sno,
                        dddjh AS Bno,
                        dddjlx AS SellType,
                        ddjffs AS DeliveryType ,
                        ddhsr AS Billdate,
                        ddshopid AS Shopid,
                        ddshopname AS Shopname,
                        ddwsckhid AS Custid,
                        ddkhzklbid  AS MemberLevel,
                        ddzsjf AS RewardIntegral ,
                        ddhj AS TotalMy ,
                        ddysjehj AS DealMy ,
                        ddyspsfy AS FeeMy ,       
                        ddkxjqb  AS CrmCash,
                        ddkzzqb AS CrmIncrement,
                        ddkhb AS CrmRed,
                        ddkjf AS CrmIntegral,
                        dddjbcsj AS UpdatedTime ,
                        ddcrmzhxsmc AS CrmAccountName ,
                        ddcrmzhpzlx  AS CrmAccountType,
                        ddcrmzhpzh AS CrmAccountVoucherNo 
                        ";

        public OrderMainRepository()
        {
            Context = new WechatMallContext();
        }


        public void AddMainInfo(OrderMainInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void ModifyMainInfo(OrderMainInfoEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }

        public OrderMainInfoEntity GetOrderMainBySno(string sno)
        {
            var condition = $"where ddlsh=@sno ";
            var sql = $"SELECT {querySql}  FROM    weiscddt  {condition}";
            return GetList(sql, new {sno}).FirstOrDefault();
        }

        public IList<OrderMainInfoEntity> GetOrders(long custid)
        {
            var condition = $"where ddwsckhid=@custid ";
            var sql = $"SELECT {querySql}  FROM    weiscddt  {condition}";
            return GetList(sql, new {custid}).ToList();
        }

        public IList<OrderMainInfoEntity> GetOrdersByPage(long custid, int pageCount, string minsno)
        {
            var condition = $"where ddwsckhid=@custid ";

            if (minsno.IsNotNullOrEmpty())
                condition = condition + "and ddlsh<@minsno";
            var sql = $"SELECT top {pageCount} {querySql}  FROM    weiscddt  {condition} order by ddlsh desc";
            return GetList(sql, new {custid, minsno}).ToList();
        }

        public IList<OrderMainInfoEntity> GetOrdersByBilldate(long custid, DateTime beginDate, DateTime endDate)
        {
            var begin = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
            var end = new DateTime(endDate.Year, endDate.Month, endDate.Day);
            var condition = $"where ddwsckhid=@custid and ddhsr>=@begin and ddhsr< DATEADD(d,1,@end) ";
            var sql = $"SELECT {querySql}  FROM    weiscddt  {condition}";
            return GetList(sql, new {custid, begin, end}).ToList();
        }
    }
}